home *** CD-ROM | disk | FTP | other *** search
/ Die Speccy' 97 / Die Speccy' 97.iso / amiga_system / the_aminet / comm / bbs / s342q07.lha / mailfwd.c < prev    next >
C/C++ Source or Header  |  1994-10-09  |  5KB  |  212 lines

  1. /*
  2. *                mailfwd.c
  3. *
  4. * Implements mail forwarding.
  5. */
  6. /*
  7. *                history
  8. *
  9. * 91Sep10 HAW    .ECM support started.
  10. * 91Feb24 HAW    Created.
  11. */
  12. #include "ctdl.h"
  13. /*
  14. *                contents
  15. *
  16. *    AddMailForward()    Add forwarding information
  17. *    KillLocalFwd()        Kills local forwarding of someone
  18. *    OpenForwarding()    Initializes forwarding code
  19. *    UpdateForwarding()    Updates the file of addresses
  20. *
  21. * Local work:
  22. *    CheckFwd()        Helps to look for a given person
  23. *    EatForwarding()        Digests a line from ctdlfwd.sys
  24. *    FreeForwarding()    Frees a record from the list of addresses
  25. *    WriteForward()        Writes a record to disk
  26. */
  27. /*
  28. * MailForward
  29. *
  30. * This contains the list of forwarding address.  If a person is not
  31. * represented in this list then no forwarding should take place.  This
  32. * forwarding includes both to other systems and within this system.
  33. */
  34. SListBase MailForward =
  35.   {
  36.   NULL, CheckFwd, NULL, FreeForwarding, EatForwarding
  37.  
  38.   };
  39. char FindLocal = FALSE;        /* ugly kludge */
  40. extern CONFIG    cfg;        /* Lots an lots of variables    */
  41. /*
  42. * CheckFwd()
  43. *
  44. * This function will check to see if the ForwardMail record under examination
  45. * represents the indicated person (who parameter), and if the record is for
  46. * a local or network forward, and match that* information against the state
  47. * of the global (kludge) variable FindLocal.  It returns NULL unless everything
  48. * matches, otherwise the address of the forwarding information.  This function
  49. * is used to search the MailForward list.
  50. */
  51. void *CheckFwd(ForwardMail *data, char *who)
  52.   {
  53.   if ((!FindLocal && data->System != NULL) ||
  54.   (FindLocal && data->System == NULL))
  55.     {
  56.     return (strCmpU(data->UserName, who) == SAMESTRING) ? data : NULL;
  57.  
  58.     }
  59.   else
  60.   return NULL;
  61.  
  62.   }
  63. /*
  64. * EatForwarding()
  65. *
  66. * This function eats a line from ctdlfwd.sys.
  67. *
  68. * Format: <username><tab><system spec><tab><alias>
  69. *    where "system spec" can be a domain-name (or nothing at all).
  70. *    "alias" can be the same as username.
  71. */
  72. void *EatForwarding(char *line)
  73.   {
  74.   ForwardMail *data;
  75.   char *tab, *tab2;
  76.   if ((tab = strchr(line, '\t')) == NULL)
  77.     {
  78.     return NULL;
  79.  
  80.     }
  81.   *tab++ = 0;
  82.   if ((tab2 = strchr(tab, '\t')) == NULL)
  83.     {
  84.     return NULL;
  85.  
  86.     }
  87.   *tab2++ = 0;
  88.   data = GetDynamic(sizeof *data);
  89.   data->UserName = strdup(line);
  90.   data->System   = (*tab) ? strdup(tab) : NULL;
  91.   data->Alias    = strdup(tab2);
  92.   CleanEnd(data->Alias);    /* seems ctdlfwd.sys occasionally */
  93.   /* gets trashed -- trailing whitespace */
  94.   return data;
  95.  
  96.   }
  97. /*
  98. * FreeForwarding()
  99. *
  100. * This frees a forwarding record for the MailForward list.
  101. */
  102. void FreeForwarding(ForwardMail *data)
  103.   {
  104.   free(data->UserName);
  105.   if (data->System != NULL)
  106.   free(data->System);
  107.   free(data->Alias);
  108.   free(data);
  109.  
  110.   }
  111. /*
  112. * OpenForwarding()
  113. *
  114. * This function opens the forwarding data structures.  It should be called
  115. * only once during Citadel (or utility) initialization.
  116. */
  117. void OpenForwarding()
  118.   {
  119.   SYS_FILE    tempName;
  120.   makeSysName(tempName, "ctdlfwd.sys", &cfg.roomArea);
  121.   MakeList(&MailForward, tempName, NULL);
  122.  
  123.   }
  124. static FILE *MailFwdFd;
  125. /*
  126. * UpdateForwarding()
  127. *
  128. * This function updates ctdlfwd.sys.
  129. */
  130. void UpdateForwarding()
  131.   {
  132.   SYS_FILE tempName;
  133.   void WriteForward();
  134.   extern char *WRITE_TEXT;
  135.   makeSysName(tempName, "ctdlfwd.sys", &cfg.roomArea);
  136.   if ((MailFwdFd = safeopen(tempName, WRITE_TEXT)) != NULL)
  137.     {
  138.     RunList(&MailForward, WriteForward);
  139.     fclose(MailFwdFd);
  140.  
  141.     }
  142.  
  143.   }
  144. /*
  145. * WriteForward()
  146. *
  147. * This function will write out a record to ctdlfwd.sys.  This is used in
  148. * conjunction with UpdateForwarding (in particular, a RunList() call).
  149. */
  150. void WriteForward(ForwardMail *data)
  151.   {
  152.   /*
  153.   * this check against NULL is present because we use the same
  154.   * file for system and non-system forwarding
  155.   */
  156.   fprintf(MailFwdFd, "%s\t%s\t%s\n", data->UserName,
  157.   (data->System == NULL) ? "" : data->System,
  158.   data->Alias);
  159.  
  160.   }
  161. /*
  162. * AddMailForward()
  163. *
  164. * This function adds a new account to the forwarding list.  If a record
  165. * exists for this account, it is deleted first.  The list on disk will
  166. * also be updated.
  167. */
  168. void AddMailForward(char *acct, char *system, char *fwdacct)
  169.   {
  170.   ForwardMail *address;
  171.   if (system == NULL) FindLocal = TRUE;
  172.   address = GetDynamic(sizeof *address);
  173.   address->UserName = strdup(acct);
  174.   address->System = (system != NULL) ? strdup(system) : NULL;
  175.   address->Alias = strdup(fwdacct);
  176.   /* have to use two steps here rather than just one */
  177.   KillData(&MailForward, acct);
  178.   AddData(&MailForward, address, NULL, FALSE);
  179.   UpdateForwarding();
  180.   FindLocal = FALSE;
  181.  
  182.   }
  183. /*
  184. * KillLocalFwd()
  185. *
  186. * This kills forwarding to a local account -- .ecm.
  187. */
  188. void KillLocalFwd(char *name)
  189.   {
  190.   FindLocal = TRUE;
  191.   KillData(&MailForward, name);
  192.   FindLocal = FALSE;
  193.   UpdateForwarding();
  194.  
  195.   }
  196. /*
  197. * FindLocalForward()
  198. *
  199. * This discovers if the given person has a local forwarding request.  If so,
  200. * the name of the account is returned.  If not, NULL is returned.
  201. */
  202. char *FindLocalForward(char *name)
  203.   {
  204.   ForwardMail *address;
  205.   FindLocal = TRUE;
  206.   address = SearchList(&MailForward, name);
  207.   FindLocal = FALSE;
  208.   if (address == NULL) return NULL;
  209.   return address->Alias;
  210.  
  211.   }
  212.